package cn.itcast.controller;

import cn.itcast.constant.MessageConstant;
import cn.itcast.domain.CheckGroup;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.QueryPageBean;
import cn.itcast.entity.Result;
import cn.itcast.service.CheckGroupService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/group")
public class CheckGroupController {

    @Reference(timeout = 5000)
    private CheckGroupService groupService;

    @RequestMapping("/findPage")
    public PageResult getPageResult(@RequestBody QueryPageBean pageBean){

        PageResult result = groupService.getPage(pageBean);

        return result;
    }

    /**
     *  页面需要返回
     * @param checkitemIds
     * @param checkGroup
     * @return
     */
    @RequestMapping("/addGroup")
    public Result addGroup(Integer[] checkitemIds , @RequestBody CheckGroup checkGroup ){
        try {
            groupService.addGroup(checkGroup,checkitemIds);
           return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
           return new Result(false,MessageConstant.ADD_CHECKGROUP_FAIL);
        }
    }

    @RequestMapping("/backData")
    public Result backData(Integer id){
        try {
            List<Integer> list = groupService.findEditById(id);
            return new Result(true,MessageConstant.EDIT_CHECKGROUP_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_CHECKGROUP_FAIL);
        }
    }

    @RequestMapping("/editByParam")
    public Result editByParam(Integer[] checkitemIds , @RequestBody CheckGroup checkGroup){
        try {
            groupService.editByParam(checkitemIds,checkGroup);
            return new Result(true,MessageConstant.EDIT_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_CHECKGROUP_FAIL);
        }
    }


    @RequestMapping("/deletegroup")
    public Result deleteGroup(Integer id){
        try {
            groupService.deleteGroup(id);
            return new Result(true,MessageConstant.DELETE_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_CHECKGROUP_FAIL);
        }
    }

    @RequestMapping("/findAll")
    public Result findPageAll(){
        try {
            List<CheckGroup> groupList= groupService.findAll();
            return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,groupList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }


}
